package com.erp.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.shop.domain.ErpSellerStoreEmpowerAutoPush;
import com.erp.shop.domain.bo.ErpSellerStoreEmpowerAutoPushBo;
import com.erp.shop.domain.vo.ErpSellerStoreEmpowerAutoPushVo;
import com.erp.shop.mapper.ErpSellerStoreEmpowerAutoPushMapper;
import com.erp.shop.service.IErpSellerStoreEmpowerAutoPushService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 店铺自动刊登配置Service业务层处理
 *
 * @author erp
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreEmpowerAutoPushServiceImpl implements IErpSellerStoreEmpowerAutoPushService {

    private final ErpSellerStoreEmpowerAutoPushMapper baseMapper;

    /**
     * 查询店铺自动刊登配置
     */
    @Override
    public ErpSellerStoreEmpowerAutoPushVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店铺自动刊登配置列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreEmpowerAutoPushVo> queryPageList(ErpSellerStoreEmpowerAutoPushBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreEmpowerAutoPush> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreEmpowerAutoPushVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店铺自动刊登配置列表
     */
    @Override
    public List<ErpSellerStoreEmpowerAutoPushVo> queryList(ErpSellerStoreEmpowerAutoPushBo bo) {
        LambdaQueryWrapper<ErpSellerStoreEmpowerAutoPush> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreEmpowerAutoPush> buildQueryWrapper(ErpSellerStoreEmpowerAutoPushBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreEmpowerAutoPush> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ErpSellerStoreEmpowerAutoPush::getUserId, bo.getUserId());
        lqw.eq(bo.getStoreId() != null, ErpSellerStoreEmpowerAutoPush::getStoreId, bo.getStoreId());
        lqw.eq(bo.getTemplateId() != null, ErpSellerStoreEmpowerAutoPush::getTemplateId, bo.getTemplateId());
        lqw.eq(StringUtils.isNotBlank(bo.getCategoryIds()), ErpSellerStoreEmpowerAutoPush::getCategoryIds, bo.getCategoryIds());
        lqw.eq(bo.getInterval() != null, ErpSellerStoreEmpowerAutoPush::getInterval, bo.getInterval());
        lqw.eq(StringUtils.isNotBlank(bo.getBrandIds()), ErpSellerStoreEmpowerAutoPush::getBrandIds, bo.getBrandIds());
        lqw.eq(bo.getStatus() != null, ErpSellerStoreEmpowerAutoPush::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增店铺自动刊登配置
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreEmpowerAutoPushBo bo) {
        ErpSellerStoreEmpowerAutoPush add = BeanUtil.toBean(bo, ErpSellerStoreEmpowerAutoPush.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改店铺自动刊登配置
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreEmpowerAutoPushBo bo) {
        ErpSellerStoreEmpowerAutoPush update = BeanUtil.toBean(bo, ErpSellerStoreEmpowerAutoPush.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreEmpowerAutoPush entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除店铺自动刊登配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
